package io.lckgroup.modules.lck.controller.wxmp;

import io.lckgroup.common.utils.R;
import io.lckgroup.modules.lck.annotation.WxLogin;
import io.lckgroup.modules.lck.annotation.WxLoginUser;
import io.lckgroup.modules.lck.entity.CustomerInfoEntity;
import io.lckgroup.modules.lck.entity.OrderMainEntity;
import io.lckgroup.modules.lck.entity.ShopFoodEntity;
import io.lckgroup.modules.lck.entity.wxmpvo.ShopFoodEntityWeixinVo;
import io.lckgroup.modules.lck.form.OrderMainEntityWeixinForm;
import io.lckgroup.modules.lck.form.WeixinPay;
import io.lckgroup.modules.lck.form.wxgzh.OrderDetailWxgzhForm;
import io.lckgroup.modules.lck.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangrui
 */
@RestController
@RequestMapping("/mp/order")
public class WeiXinOrderController {

    /**
     * 功能描述: <br>
     * 〈〉提交订单
     *
     * @Param: [data]
     * @Return: io.lckgroup.common.utils.R
     * @Author: 13980
     * @Date: 2020/2/29 17:31
     */

    @Autowired
    private OrderService orderService;

    /**
     * 功能描述: <br>
     * 〈〉生成订单
     *
     * @Param: [data, customerInfoEntity]
     * @Return: io.lckgroup.common.utils.R
     * @Author: 13980
     * @Date: 2020/3/18 13:47
     */
    @WxLogin
    @PostMapping("/weixinMakeOrder")
    public R makeOrder(@RequestBody OrderMainEntityWeixinForm data, @WxLoginUser CustomerInfoEntity customerInfoEntity) {
        data.setCustomerId(customerInfoEntity.getId());
        // 判断是否有下架的菜品 和已删除的菜品
        List<ShopFoodEntityWeixinVo> foodList = data.getFoodList();
//        菜品ID集合
        List<Long> idList = new ArrayList<Long>();
        for (ShopFoodEntityWeixinVo shopFoodEntityWeixinVo : foodList) {
            idList.add(shopFoodEntityWeixinVo.getId());
        }
        List<ShopFoodEntity> isuper = orderService.isuper(idList);
        if (isuper.size() > 0) {
            return R.error("有未上架商品").put("data", isuper);
        }
//        是否打烊 true （==1）营业   false （==0）打烊
        boolean isOpen = orderService.isMerchinfoOpen(data.getMerchantId());
        if (!isOpen) {
            return R.error("商家已经打烊");
        }
        //生成订单
        OrderMainEntity mainEntity = orderService.makeOrder(data);
        if (mainEntity == null) {
            return R.error("菜品信息已更新，请重新下单");
        } else {
            return R.ok().put("data", mainEntity);
        }
    }

    /**
     * 功能描述: <br>
     * 〈〉请求订单列表
     *
     * @Param: [customerInfoEntity, MerchantInfoId]
     * @Return: io.lckgroup.common.utils.R
     * @Author: 13980
     * @Date: 2020/3/9 13:47
     */
    @WxLogin
    @GetMapping("/weixinOrderList/{MerchantInfoId}")
    public R weixinGetOrderList(@WxLoginUser CustomerInfoEntity customerInfoEntity, @PathVariable Long MerchantInfoId) {
        List<OrderMainEntity> orderList = orderService.getOrderList(customerInfoEntity.getId(), MerchantInfoId);
        return R.ok().put("data", orderList);
    }

    /**
     * 功能描述: <br>
     * 〈〉根据订单号查询订单明细
     *
     * @Param: [orderMainId]
     * @Return: io.lckgroup.common.utils.R
     * @Author: 13980
     * @Date: 2020/3/18 13:49
     */
    @WxLogin
    @GetMapping("/weixinGetOrderDetail")
    public R weixinGetOrderDetail(Long orderMainId) {
        List<OrderDetailWxgzhForm> detail = orderService.getOrderDetailWxmp(orderMainId);
        return R.ok().put("data", detail);
    }

    /**
     * 功能描述: <br>
     * 〈〉订单支付
     *
     * @Param: [pat]
     * @Return: io.lckgroup.common.utils.R
     * @Author: 13980
     * @Date: 2020/3/18 13:50
     */
    @WxLogin
    @PostMapping("/weixinOrderId")
    public R weixintoPay(@RequestBody WeixinPay pat) {
        String url = null;
        if (pat.getMerchantId() != 0 && pat.getOrderId() != null) {
            url = orderService.topayweixin(pat.getOrderId(), pat.getMerchantId());
        }
        return R.ok().put("data", url);
    }

    /**
     * 功能描述: <br>
     * 〈〉重新调用支付（重新生成订单号）
     *
     * @Param: [pat      ]
     * @Return: io.lckgroup.common.utils.R
     * @Author: 13980
     * @Date: 2020/3/18 13:51
     */
    @WxLogin
    @PostMapping("/weixinPay")
    public R weixintoSecondPay(@RequestBody WeixinPay pat) {
//        String url = null;
        Map<String, String> map = new HashMap<>();
        OrderMainEntity entity = orderService.updateOrderId(pat.getId(), pat.getMerchantId());
        System.out.println(entity+"---------");
//        判断是否过期
        if(entity.getIsPay()==2){
            return R.error("订单已过期");
        }
//        重新支付之前还需要去判断一下当前订单中是否有下架的菜和商家是否打烊
        if (pat.getMerchantId() != 0 && entity.getOrderId() != null) {
            String url = orderService.topayweixin(entity.getOrderId(), pat.getMerchantId());
            map.put("url", url);
            map.put("newOrderMainId", entity.getOrderId());
        }
        List<Long> foodIdList = orderService.getFoodIdList(entity.getId());
        List<ShopFoodEntity> isuper = orderService.isuper(foodIdList);
        if (isuper.size() > 0) {
            return R.error("有未上架商品");
        }
        //        是否打烊 true （==1）营业   false （==0）打烊
        boolean isOpen = orderService.isMerchinfoOpen(pat.getMerchantId());
        if (!isOpen) {
            return R.error("商家已经打烊");
        } else {
            return R.ok().put("data", map);
        }
    }

}
